


$(document).ready(function(){

    defaultTemplate = Handlebars.compile($('#defaultTemplate').html());
    totalTemplate = Handlebars.compile($('#totalTemplate').html());
    template = Handlebars.compile($('#defaultTemplate').html());
    template2 = Handlebars.compile($('#jazzyRequiredTemplate').html());
    template3 = Handlebars.compile($('#partyBagsTemplate').html());
    var totTemplate = ({
        totalCost: '80.00'
    })
    var defTemplate = ({
        numberOfChildren : '10',
        packageChosen : 'Small 10 - 15 children',
        numberOfSandwichPlatters : '0',
        numberOfDipsPlatters : '0',
        jazzySelected : 'No Thanks',
        partyBagsSelected : 'No Thanks',

        packageCost : '8.00',
        costOfChildren : '80.00',
        totalCost : '80.00',
        totalNumberOfPlatters : '0',
        totalCostOfPlatters : '0.00'

    });
    $('#rightContent').html(defaultTemplate(defTemplate));
    $('#costContainer').after(totalTemplate(totTemplate));

   $('#bookingForm').submit(function(){

       var ajax = $.ajax({
           url: 'booking/update/validate',
           type: 'POST',
           dataType: 'json',
           data: getData('verifySubmission'),
          success: function(data){
              console.log(data);
            if(data.isValid){
               alert("Thank you for your booking. Please read the booking information sent to your email address");
            }
            else{
               alert(data.errorString);
            }
          }
       });
       return false;
   });



    jQuery(function($) {
            $("#datepicker").datepicker();
     });

    $(function(){
        $('#childSlider').slider({
            max: 30,
            min: 10,
            value: 10,
            slide: function(e,ui) {
                $('#childSliderValue').html(ui.value);
            },

            change: function(e,ui){
                var ajax = $.ajax({
                    url: 'booking/update/validate',
                    type: 'POST',
                    dataType: 'json',
                    data: getData("cost"),
                    success: function(data){
                        console.log(data);
                        printContent(data);
                    }
                });
                return false;
            }
        });
        });

    $(function(){
        $('#adultSlider').slider({
            max: 25,
            min: 4,
            value: 4,
            slide: function(e,ui) {
                $('#adultSliderValue').html(ui.value);
            }
        });
    });

    $(function(){
        $('#adultPlatterDipSlider').slider({
            max: 10,
            min: 0,
            value: 0,
            slide: function(e,ui) {
                $('#adultPlatterDipSliderValue').html(ui.value);
            },

            change: function(e,ui){
                var ajax = $.ajax({
                    url: 'booking/update/validate',
                    type: 'POST',
                    dataType: 'json',
                    data: getData("cost"),
                    success: function(data){
                        console.log(data);
                        printContent(data);
                    }
                });
                return false;
            }
        });
    });

    $(function(){
        $('#adultPlatterSandwichSlider').slider({
            max: 10,
            min: 0,
            value: 0,
            slide: function(e,ui) {
                $('#adultPlatterSandwichSliderValue').html(ui.value);
            },

            change: function(e,ui){
                var ajax = $.ajax({
                    url: 'booking/update/validate',
                    type: 'POST',
                    dataType: 'json',
                    data: getData("cost"),
                    success: function(data){
                        console.log(data);
                        printContent(data);
                    }
                });
                return false;
            }
        });
    });

    $(function(){
        $('#jazzyRequired,#partyBags').change(function(e,ui){
            var ajax = $.ajax({
                url: 'booking/update/validate',
                type: 'POST',
                dataType: 'json',
                data: getData("cost"),
                success: function(data){
                    console.log(data);
                    printContent(data);
                }
            });
            return false;
        });
    });

    $(function(){
        $('#childAgeSlider').slider({
            max: 10,
            min: 1,
            value: 1,
            slide: function(e,ui) {
                $('#childAgeSliderValue').html(ui.value);
            }
        });
    });

    function getData(requestType){
    var data = $('#bookingForm').serializeArray();
        if(requestType === 'cost'){
            data.push({name: 'requestType', value:("calculateCost")});
        }
        else{data.push(data.push({name: 'requestType', value:("verifySubmission")}));}

        data.push({name: 'numberOfChildren', value: $("#childSliderValue").html()});
        data.push({name: 'numberOfAdults', value: $("#adultSliderValue").html()});
        data.push({name: 'dipPlatters', value: $("#adultPlatterDipSliderValue").html()});
        data.push({name: 'sandwichPlatters', value: $("#adultPlatterSandwichSliderValue").html()});
        data.push({name: 'childAge', value: $("#childAgeSliderValue").html()});
    return data;
    }


    function printContent(data){
        console.log(template(data));
    $('#rightContent').html(template(data));
        if(data.partyBags > 0){
            $('#costSummary tr:last').after(template3(data));
        }
        if(data.jazzyRequired > 0){
                $('#costSummary tr:last').after(template2(data));
            }

        $('#costContainer').after(totalTemplate(data));
    }
});






